package com.usian.service.impl;

import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.commom.vo.ResultCommonVo;
import com.usian.mapper.UserMapper;
import com.usian.service.UserService;
import org.springframework.stereotype.Service;

import com.usian.commom.pojo.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2023 11 22 09 30
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public List<User> findUser(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);

//        LambdaQueryWrapper<User> lambda = userQueryWrapper.lambda();
//        lambda.eq(StringUtils.isNotColumnName(username),User::getUsername,username);

        List<User> users = this.baseMapper.selectList(userQueryWrapper);


        return users;
    }

    @Override
    public List<User> findUserByUser(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();

        //StringUtils.isNotColumnName(user.getUsername()):条件判断 只有不为空的时候，才查询
        //User::getUsername  对象得username
        //user.getUsername()：获取的是传过来的值


        userQueryWrapper.lambda()
                .eq(StringUtils.isNotColumnName(user.getUsername()),User::getUsername,user.getUsername())
                .eq(StringUtils.isNotColumnName(user.getPassword()),User::getPassword,user.getPassword())
                .eq(User::getAge,user.getAge());


        List<User> users = this.baseMapper.selectList(userQueryWrapper);

        return users;
    }

    @Override
    public Page<User> findUserPage(User user,Integer pageNum,Integer pageSize) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getUsername,user.getUsername());


        Page<User> objectPage = new Page<User>(pageNum,pageSize);


        Page<User> userPage = this.baseMapper.selectPage(objectPage, userQueryWrapper);


        return userPage;
    }

    @Override
    public List<User> findUserByUsernameANdage(String username, Integer age) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getUsername,username)
                .eq(User::getAge,age);

        List<User> users = this.baseMapper.selectList(userQueryWrapper);
        return users;
    }

    @Override
    public ResultCommonVo login(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getUsername,user.getUsername())
                .eq(User::getPassword,user.getPassword());
        User user1 = this.baseMapper.selectOne(userQueryWrapper);

        HashMap<String, Object> resMap = new HashMap<>();

        if(null!=user1){

            //生成token
            Map<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("uid",user1.getId());
            stringObjectHashMap.put("e_time",1000*60*60*2);
            String token = JWTUtil.createToken(stringObjectHashMap, "111".getBytes());
            resMap.put("user",user1);
            resMap.put("token",token);
            return ResultCommonVo.success(resMap);
        }else {
            return ResultCommonVo.error("登陆失败");
        }



    }
}
